package com.practice.car.cardataapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.practice.car.cardataapp.entity.Subcar;
import com.practice.car.cardataapp.entity.rearEnd.ConditionSubCarForm;
import com.practice.car.cardataapp.entity.rearEnd.UserSubCarForm;
import com.practice.car.cardataapp.mapper.SubCarDao;
import com.practice.car.cardataapp.service.SubCarService;
import com.practice.car.cardataapp.util.R;
import lombok.Data;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;

/**
 * car-data-app project
 *
 * @author ZhangMingXing
 * @date 2020/9/13
 */
@Service
@Data
public class SubCarServiceImpl extends ServiceImpl<SubCarDao, Subcar> implements SubCarService {

	private UserSubCarForm userSubCarForm = null;

	/*
	//用于封装前端传递的条件查询对象
	private String powerType;  //动力类型，都有 7数据
	private Double output;    //排量，默认值为0，需要进行过滤 42数据
	private String type;      //都有 122数据
	private String auto;       //不都有，默认为-，需要进行过滤  12+数据
	private Integer  seat;     //都有 20+数据
	private Integer priceSection;//价格区间 前端直接提供固定选项
	 */
	private void init() {
		userSubCarForm = new UserSubCarForm();
		List<Subcar> list = this.list();
		HashSet<Double> output = new HashSet<>();
		HashSet<Integer> seat = new HashSet<>();
		list.forEach(e->{
			userSubCarForm.getAuto().add(e.getAuto());
			userSubCarForm.getAuto().add("");
			output.add(e.getOutput());
			userSubCarForm.getPowerType().add(e.getPowerType());
			userSubCarForm.getPowerType().add("");
			seat.add(e.getSeat());
			userSubCarForm.getType().add(e.getType());
			userSubCarForm.getType().add("");
		});
		output.add(0.0);
		seat.add(0);
		userSubCarForm.getOutput().addAll(output);
		userSubCarForm.getOutput().sort((a,b)->{
			if (a < b) {
				return -1;
			}
			return 1;
		});
		userSubCarForm.getSeat().addAll(seat);
		userSubCarForm.getSeat().sort((a,b)->a-b);
	}
	@Override
	public R getChoice(ConditionSubCarForm form, Integer current) {
		if (userSubCarForm == null) {
			init();
		}
		System.out.println("form = " + form);
		LambdaQueryWrapper<Subcar> query = Wrappers.lambdaQuery();
		this.powerType(query, form)
				.output(query, form)
				.type(query, form)
				.auto(query, form)
				.seat(query, form)
				.priceSection(query, form);
		Page<Subcar> page = this.page(new Page<>(current, 20), query);
		return R.create().put("total",page.getTotal())
				.put("list",page.getRecords())
				.put("usersubcarform",this.userSubCarForm);
	}

	public SubCarServiceImpl powerType(LambdaQueryWrapper<Subcar> query, ConditionSubCarForm form) {
		String powerType = form.getPowerType();
		if (!"".equals(powerType)) {
			query.eq(Subcar::getPowerType, powerType);
		}
		return this;
	}
	public SubCarServiceImpl type(LambdaQueryWrapper<Subcar> query, ConditionSubCarForm form) {
		String type = form.getType();
		if (!"".equals(type)) {
			query.eq(Subcar::getType, type);
		}
		return this;
	}

	public SubCarServiceImpl auto(LambdaQueryWrapper<Subcar> query, ConditionSubCarForm form) {
		String auto = form.getAuto();
		if (!"".equals(auto)) {
			query.eq(Subcar::getAuto, auto);
		}
		return this;
	}

	public SubCarServiceImpl output(LambdaQueryWrapper<Subcar> query, ConditionSubCarForm form) {
		Double output = form.getOutput();
		if (0!=output) {
			query.eq(Subcar::getOutput, output);
		}
		return this;
	}

	public SubCarServiceImpl seat(LambdaQueryWrapper<Subcar> query, ConditionSubCarForm form) {
		Integer seat = form.getSeat();
		if (0!=seat) {
			query.eq(Subcar::getSeat, seat);
		}
		return this;
	}

	public SubCarServiceImpl priceSection(LambdaQueryWrapper<Subcar> query, ConditionSubCarForm form) {
		Integer priceSection = form.getPriceSection();
		if (0!=priceSection) {
			if (priceSection == 10) {
				query.gt(Subcar::getPriceSection, priceSection);
			}else {
				query.between(Subcar::getPriceSection, priceSection, priceSection + 1);
			}
		}
		return this;
	}
}
